import { addPointApi } from "../api/user";

export const aboutUser = {
  data: {},

  saveUserData(saveData) {
    this.data = { ...this.data, ...saveData };
    this.saveStorage();
  },

  saveStorage() {
    tt.setStorageSync("userInfo", JSON.stringify(this.data));
  },

  getUserInfo() {
    const res = tt.getStorageSync("userInfo") && JSON.parse(tt.getStorageSync("userInfo"));

    this.data = res || {};

    return this.data;
  },

  async addPointFun(type, watchTime = 0) {
    let allParam = JSON.stringify(this.data?.query);

    let params = {
      uid: this.data.userId,
      appId: this.data?.refererInfo?.appId,
      allParam,
    };

    switch (type) {
      case "openNum":
        params.openNum = 1;
        break;
      case "watchNum":
        params.watchNum = 1;
        break;
      case "watchTime":
        params.watchTime = watchTime;
        break;
      case "watchGg":
        params.watchGg = 1;
        break;
    }

    console.log("埋点接口参数", params);

    try {
      await addPointApi(params);
    } catch (err) {
      console.log("埋点接口异常", err);
    }
  },
};
